package org.ptkparty.client.activity;

import org.ptkparty.client.ClientFactory;
import org.ptkparty.client.Context;
import org.ptkparty.client.place.PlayerDto;
import org.ptkparty.client.place.ProfilPlace;
import org.ptkparty.client.view.profil.ProfilView;
import org.ptkparty.shared.dto.ProfilDto;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;

public class ProfilActivity implements Activity {

	private static Driver driver = GWT.create(Driver.class);

	interface Driver extends RequestFactoryEditorDriver<PlayerDto, ProfilView> {
	}

	private ClientFactory clientFactory;
	private ProfilView view;
	private String player;

	public ProfilActivity(ClientFactory clientFactory, String selectedChampionship, String player) {
		this.clientFactory = clientFactory;
		Context.get().setCurrentChampionship(selectedChampionship);
		this.player = player;
	}

	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view = clientFactory.getProfilView();
		view.setPresenter(this);
		panel.setWidget(view);

		getAndDriveCurrentUser();

		clientFactory.getPtkService().getProfileForCurrentPlayer(player, Context.get().getCurrentChampionship(), new AsyncCallback<ProfilDto>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(ProfilDto result) {
				view.showTopWinner(result.getWinners());
				view.showTopLooser(result.getLosers());
				view.showTopResult(result.getTopPlayers());
				view.showKillers(result.getKillers());
				view.showVictims(result.getVictims());
				view.buildScoreStats(result.getPlayerResults());
			}
		});
	}

	private void getAndDriveCurrentUser() {
		clientFactory.getPtkService().getUser(player, new AsyncCallback<PlayerDto>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(PlayerDto result) {
				driver.initialize(clientFactory.getEventBus(), clientFactory.getFactory(), view);

				driver.display(result);
				view.setImages(result.getImage(), result.getCover());
				view.setLogoutUri(result.getLogoutUrl());
			}

		});
	}

	public String mayStop() {
		return null;
	}

	public void onCancel() {
	}

	public void onStop() {
	}

	public void connectOnGooglePlus(String clientToken) {
		clientFactory.getPtkService().logUserOnGplus(clientToken, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(Boolean result) {
				getAndDriveCurrentUser();
			}
		});
	}

	public void connectOnFacebook(String clientToken) {
		clientFactory.getPtkService().logUserOnFacebook(clientToken, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(Boolean result) {
				getAndDriveCurrentUser();
			}
		});
	}

	public void playerSelected(String userId) {
		clientFactory.getPlaceController().goTo(new ProfilPlace(Context.get().getCurrentChampionship(), userId));
	}

}
